#include "fxcc/core/graph/opengl3/ImGuiGlfwApp.h"
#include "fxcc/core/graph/common/Geometry.h"
#include "fxcc/imgui/Editor.h"

#include "fxcc/core/graph/common/ImageMipmap.h"
#include "fxcc/core/graph/common/Resources.h"
#include "fxcc/core/graph/opengl3/Texture2DJack.h"
#include "fxcc/core/graph/opengl3/Texture2DArrayJack.h"
#include "fxcc/core/graph/opengl3/Texture3DJack.h"
#include "fxcc/core/graph/opengl3/TextureCubeArrayJack.h"
#include "fxcc/core/graph/opengl3/MeshJack.h"
#include "fxcc/core/graph/opengl3/IndexBuffer.h"
#include "fxcc/core/graph/opengl3/RenderTargetJack.h"

#include "fxcc/core/AssertManager.h"

using namespace fxcc::platform;
using namespace fxcc::graph::common;
using namespace fxcc::graph::opengl3;
using namespace fxcc::imgui::graph::opengl3;
using BaseApp = fxcc::imgui::graph::opengl3::GlfwApp;

#undef main

struct SimpleApp : public BaseApp
{
public:

    std::shared_ptr<ImageMipmap> m_ImageMipMap;

    std::shared_ptr<fxcc::graph::opengl3::MeshJack> m_QuadMesh;

    std::shared_ptr<fxcc::graph::opengl3::Effect3d> m_Effect;

    std::shared_ptr<fxcc::graph::opengl3::Effect3d> m_ArrayEffect;

    std::shared_ptr<fxcc::graph::opengl3::Texture2DArrayJack> m_TexArray;

    std::vector<std::shared_ptr<fxcc::graph::opengl3::Texture2DJack>> m_Textures;

    glm::vec4 color;

    fxcc::graph::common::Camera camera;

    SimpleApp(const fxcc::platform::common::App::Desc &desc) : BaseApp(desc), color(1.0)
    {
    }
    bool Init() override
    {
        if (!BaseApp::Init())
        {
            return false;
        }

        if (!InitResources())
        {
            return false;
        }

        return true;
    };

    void printOpenGLESVersion()
    {
        const char *version = (const char *)glGetString(GL_VERSION);
        std::cout << "OpenGL ES Version: " << version << std::endl;
    };

    int useArray{ 0 };

    bool InitResources()
    {
        ImageMipmap::MipMapData image(1024, 1024, 4, 2);
        fxcc::graph::opengl3::RenderTargetJack::Desc desc(1, image);


        camera.SetPosition(glm::vec3(5.0f));
        camera.LookAt(glm::vec3(0.0f));

        auto& am = fxcc::AssertManager::Inst();

        m_QuadMesh = std::make_shared<fxcc::graph::opengl3::MeshJack>(Geometry::CreateQuad());


        {
            fxcc::graph::opengl3::Texture2DArrayJack::Desc desc1;
            //desc1.SetDDSPath(fxcc::AssertManager::GetRelativeFile("Texture/output/sunset.dds"));
            desc1.SetDDSPath(fxcc::AssertManager::GetRelativeFile("sdf.dds"));
            m_TexArray = std::make_shared<fxcc::graph::opengl3::Texture2DArrayJack>(desc1);

        }
        {
            const std::string fragCode = R"(

out vec4 FragColor;

uniform sampler2D diffuse;

void main()
{
    vec4 diffuseColor = texture(diffuse, TexCoord);
    FragColor = diffuseColor ;  
}
)";
            m_Effect = std::make_shared<fxcc::graph::opengl3::Effect3d>(fragCode);

        }

        m_TexArray->GenerateTextures();

        for (int i = 0; i < m_TexArray->m_Textures.size(); i++)
        {
            m_TexArray->m_Textures[i]->SaveImage(std::string("sdfout/Demo2") + std::to_string(i));
        }
        return true;
    };

    fxcc::imgui::Editor editor;

    void OnEdit() override
    {
        ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());

        ImGui::Begin("TextureArray");
        editor.CheckBoxInst<int>("useArray", useArray);
        //ImGui::DragInt("index", &index, 1.0f, 0, 5);

        for (int i=0;i<m_TexArray->m_Textures.size();i++)
        {
            const auto texture = m_TexArray->m_Textures[i];
            std::string l2 = std::to_string(i);
            ImGui::Text(l2.c_str());

            if (texture)
            {
                ImGui::Image(texture->GetTextureId(), { 200,200 });
            }
        }
        ImGui::End();

        ImGui::Begin("Framebuffer");
        editor.AddFrameBuffer(m_RTJack->m_Textures[0]->GetTextureId());
        ImGui::End();
    }
    std::shared_ptr<fxcc::graph::opengl3::RenderTargetJack> m_RTJack;

    void OnDestroy() override
    {
      
        BaseApp::OnDestroy();
    }
    void OnFrameRender() override
    {
        glViewport(0, 0, m_Desc.m_Size.x, m_Desc.m_Size.y);

        glClearColor(color.x, color.y, color.z, color.w);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    };

    void OnRenderTextureArray(int index)
    {


    }
    void OnFramebuffersize(int w, int h) override
    {
        BaseApp::OnFramebuffersize(w, h);
        camera.SetAspect(m_Desc.Aspect());
        fxcc::graph::opengl3::RenderTargetJack::Desc desc( 1, {w, h, 3,0});
        m_RTJack = std::make_shared<fxcc::graph::opengl3::RenderTargetJack>();


    }
    void OnUpdate() override
    {
        BaseApp::OnUpdate();

        float ctTime = m_GlfwlTimer.m_CurrentTime;
        camera.FreeMove(m_Input, m_GlfwlTimer.m_DeltaTime);

        float deltaTime = m_GlfwlTimer.m_DeltaTime;


    }
};

int main()
{
    ztclog::pushFunction([](const ztclog::Line &line)
                         {
		if (line.m_Type != ztclog::Line::Type::_line_debug_)
		{
			std::cout << line.m_Text << std::endl;
		} });

    using _App = SimpleApp;
    using _Desc = fxcc::platform::common::App::Desc;

    _Desc desc;
    desc.m_Pos = glm::ivec2(100, 100);
    desc.m_Size = glm::ivec2(900, 600);
    desc.m_ScaleFactor = 1.0f;

    desc.m_Title = "jack data";
    std::shared_ptr<_App> _app = std::make_shared<_App>(desc);
    return _app->Run();
};
